package com.srzk.youthleagueback.mapper;

import com.srzk.youthleagueback.pojo.Lecture;
import com.srzk.youthleagueback.utils.ObjectUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class LectureProvider extends SQL {
    private static final String TABLE_NAME = "lecture";

    public String updateLecture(@Param("lecture") Lecture lecture) {
        return new SQL() {{
            UPDATE(TABLE_NAME);
            if (!StringUtils.isEmpty(lecture.getUser())) {
                SET("user = #{user}");
            }
            if (!StringUtils.isEmpty(lecture.getYear())) {
                SET("year= #{year}");
            }
            if (!StringUtils.isEmpty(lecture.getId().toString())) {
                SET("id= #{id}");
            }
            if (!ObjectUtils.isEmpty(lecture.getDate())) {
                SET("date= #{date}");
                SET("address= #{address}");
                SET("theme= #{theme}");
                SET("mainContent= #{mainContent}");
                SET("count= #{count}");
            }
            WHERE("id = #{id}");
            WHERE("user = #{user}");
            WHERE("year= #{year}");
        }}.toString();
    }

    public String insertLecture(@Param("lecture") Lecture lecture){

        return new SQL() {{
            INSERT_INTO(TABLE_NAME);
            try {
                Map<String, Object> data = ObjectUtil.reflect(lecture);
                data.forEach((key, value) -> {
                    if (value != null) {
                        VALUES(key, "\'"+value.toString()+"\'");
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }}.toString();
    }

}
